package com.juzhi.dvrapp.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by YangHu on 2017/5/3.
 */

public class FileUtils {

    public final static String LOG_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/WisdomCare/Log/";
    public final static String IMAGE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/WisdomCare/image/";
    public final static String VIDEO_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/WisdomCare/video/";

    static {
        File logDir = new File(LOG_PATH);
        if (!logDir.exists())
            logDir.mkdirs();

        File imageDir = new File(IMAGE_PATH);
        if (!imageDir.exists())
            imageDir.mkdirs();

        File videoDir = new File(VIDEO_PATH);
        if (!videoDir.exists())
            videoDir.mkdirs();
    }

    /**
     * 判断SD卡状态
     *
     * @return
     */
    public static boolean isSDCardMounted() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    /**
     * 保存图片到sdcard
     *
     * @param bitmap
     * @param imageName
     */
    public static String savePicture(Bitmap bitmap, String imageName) {
        if (!isSDCardMounted()) {
            return null;
        }
        FileOutputStream b = null;
        File imageDir = new File(IMAGE_PATH);
        if (!imageDir.exists())
            imageDir.mkdirs();

        String fileName = IMAGE_PATH + "/" + imageName;
        try {
            b = new FileOutputStream(fileName);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                b.flush();
                b.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return fileName;
    }

   /* public static boolean deleteDir(File dir) {
        if (!dir.exists() || !dir.isDirectory()) {
            return false;
        }
        if (dir.isDirectory()) {
            File[] files = dir.listFiles();
            //递归删除目录中的子目录下
            for (int i = 0; i < files.length; i++) {
                if (files[i].isFile()) {
                    //删除子文件
                    flag = deleteFile(files[i].getAbsolutePath());
                    if (!flag) break;
                } else {
                    //删除子目录
                    flag = deleteDirectory(files[i].getAbsolutePath());
                    if (!flag) break;
                }
            }
        }
        // 目录此时为空，可以删除
        return true;
    }*/

    /**
     * 删除文件夹下所有文件
     *
     * @return
     */
    public static void deleteDirectoryAllFile(String directoryPath) {
        final File file = new File(directoryPath);
        deleteDirectoryAllFile(file);
    }

    public static void deleteDirectoryAllFile(File file) {
        if (!file.exists())
            file.mkdirs();
        else {
            final File files[] = file.listFiles();
            for(File childs : files ){
                childs.delete();
            }
        }
    }

    public static void deleteLogAndImg() {
        deleteDirectoryAllFile(LOG_PATH);
        deleteDirectoryAllFile(IMAGE_PATH);
    }

    /**
     * 发送广播 刷新图片
     *
     * @param context
     * @param photo
     */
    public static void scanMediaFile(Context context, File photo) {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri contentUri = Uri.fromFile(photo);
        mediaScanIntent.setData(contentUri);
        context.sendBroadcast(mediaScanIntent);
    }
}
